// Script executed in dash.html

$(function () {

    addCourselist();

    addCourseContent();

});


function addCourselist() {
    // Todo: Get list of recent courses from server
    if(courses) {    
        courses.forEach(function (item) {
            var css = { "background-image": "url('" + item.icon + "')" };
            $("<button>").text(item.name).css(css).appendTo($(".citems"));
        });
    }
}

function addCourseContent() {
    
    // Todo: Get course data from server
    
    if(courseData) {
    
        var $subhead = $(".dashtab .dtsubhead"); 
        var $rowtemp = $(".dashtab .dtdatarow").remove().removeClass("hidden");
        var $tab = $subhead.find(".dttitle").text(courseData.title).end().parent();
        
        var tCompletion = 0, tExpertise = 0, tExpTotal = 0;
        
        var setProgress = function($div, percentage) { 
            $div.find("span span").css("width", percentage + "%");
        }
        
        var fix = function(num) { 
            return ((num=Math.floor(num)%100)<10?"0":"")+num; 
        }        
        
        courseData.chapters.forEach(function(chapter, i) {
            var $row = $rowtemp.clone();                                
            var tState = function() { $(this).toggleClass("max"); }
            
            var txt = "CHAPTER " + (i+1) + " :" +chapter.title;
            
            tCompletion += chapter.completion;
            setProgress($row.find(".dtprogress"), chapter.completion);
            
            tExpertise += chapter.expertise;
            tExpTotal += chapter.subs.length;
            setProgress($row.find(".dtscore"), 
                        chapter.expertise/ chapter.subs.length * 100);
            
            $rowt = $row.find(".dttitle").click(tState).html($("<div>").text(txt));
            
            chapter.subs.forEach(function(sub) {
                $("<div>").text("OBJECTIVE : " + sub).appendTo($rowt);
            });
            
            $row.appendTo($tab);
        });
        
        setProgress($subhead.find(".dtprogress"),
                    tCompletion/courseData.chapters.length);                
        $subhead.find(".dtscore > span").first().text(fix(tExpertise))
                .siblings().last().text(fix(tExpTotal));
    }
}


/*====================================================*/
/*  Dummy Data to be replaced by data from the server */
/*====================================================*/

var courses = [{ name: "Microsoft Excel", icon: "img/courses/xls.jpg" },
                   { name: "Microsoft Word", icon: "img/courses/doc.jpg" },
                   { name: "Microsoft Powerpoint", icon: "img/courses/ppt.jpg" },
                   { name: "Adobe Photoshop", icon: "img/courses/ps.jpg" },
                   { name: "Google Docs", icon: "img/courses/gd.jpg" }];

var courseData = {
    title: "Microsoft Excel Basics",
    chapters: [
        {
            title: "Conditional formatting",
            completion: 100,
            expertise: 2,
            subs: ["A", "B", "C"]
        },
        {
            title: "Charts And Graphs",
            completion: 40,
            expertise: 1,
            subs: ["Bar Charts", "Pie Charts", "Pivot Tables"]
        },
        {
            title: "Conditional formatting",
            completion: 22,
            expertise: 1,
            subs: ["A", "B", "C"]
        }
    ]
};
